<?php
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 *
	 *	Autocredit
	 *	Copyright (C) 2012, Brent Flap <blog.cyberdevil.nl>
	 *
	 *	This file is part of Autocredit.
	 *
	 *	Autocredit is free software: you can redistribute it and/or modify
	 *	it under the terms of the GNU General Public License as published by
	 *	the Free Software Foundation, either version 3 of the License, or
	 *	any later version.
	 *
	 *	This program is distributed in the hope that it will be useful,
	 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
	 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	 *	GNU General Public License for more details.
	 *
	 *	You should have received a copy of the GNU General Public License
	 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	 *
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	class spaarenwin extends Plugins{
		private static $patterns = array(
			"/input type=submit value=\"Verwijder\">(.*?)<\/table>/si",
			"/runner.php\?IM=([a-zA-Z0-9]*)./i",
			"/runner.php\?EA=([0-9]*)/i",
			"/Uitloggen/i",
			"/Welkom (.*?),/i",
			"/\x80 ([0-9.]*)<br>/i",
		);
		private static $plugin_name = "Spaar en Win";
		private static $cookie_name = "spaarenwin.cookie";
		function init(){
			message(self::$plugin_name . " loaded","COMMENT");
			return true;
		}
		function action(){
			$counter = 0;
			$once = false;
			$ar_buffer = self::getIDs("spaarenwin_mailids");
			$curl_options = array(CURLOPT_COOKIEFILE => DATA_PATH.self::$cookie_name); // Read the cookie
			$trigger = false;
			
			$login = self::getPage("http://www.spaarenwin.nl/pages/enter.php", $curl_options);
			if(self::Parse(self::$patterns[3], $login, 0) != "Uitloggen"){
				message("Signed out!","ERROR");
				$once = false;
				$postfields = "username=".SPAARENWIN_USER."&password=".SPAARENWIN_PASS;
				$curl_options = array(
					CURLOPT_POST 		=> true,
					CURLOPT_POSTFIELDS	=> $postfields,
					CURLOPT_COOKIEJAR	=> DATA_PATH.self::$cookie_name, // Write the cookie
					CURLOPT_RETURNTRANSFER	=> true,
					CURLOPT_FOLLOWLOCATION	=> 1
				);
				$login = self::getPage("http://www.spaarenwin.nl/pages/enter.php", $curl_options);
			}
			if(self::Parse(self::$patterns[3], $login, 0) == "Uitloggen" && $once == false){
				message("Succesfully signed in as ".self::Parse(self::$patterns[4], $login, 1).".","NOTICE");
				message("Credit: ".self::Parse(self::$patterns[5], $login, 1),"NOTICE");
				$once = true;
			}
			/* Now we are logged in */
		
			$content = self::getPage("http://www.spaarenwin.nl/pages/inbox.php",$curl_options);
			$mails = self::Parse(self::$patterns[0],$content,1);
			$links = explode("</tr>",$mails);
			foreach($links as $l){
				$mail_id = self::Parse(self::$patterns[1],$l,1);
				$mail_title = self::Parse("/inbox>(.*?)<\/a>/i",$l,1);
				if(!in_array($mail_id,$ar_buffer)){
					$curl_options = array(CURLOPT_COOKIEFILE => DATA_PATH.self::$cookie_name); // Read the cookie
					$content = self::getPage("http://www.spaarenwin.nl/scripts/runner.php?IM=".$mail_id.".",$curl_options);
					$hash_stuff = self::Parse(self::$patterns[2],$content,1);
					$final_link = "http://www.spaarenwin.nl/scripts/runner.php?EA=".$hash_stuff;
			
					$curl_options = array(
						CURLOPT_FOLLOWLOCATION	=> true,
						CURLOPT_RETURNTRANSFER	=> true,
						CURLOPT_COOKIEFILE 	=> DATA_PATH.self::$cookie_name,
						CURLOPT_SSL_VERIFYHOST	=> false,
						CURLOPT_SSL_VERIFYPEER	=> false
					);
					$content = self::getPage($final_link,$curl_options);
					$counter++;
					message($counter." Read email: ".$mail_id." ".$mail_title,"COMMENT");
					$ar_buffer[] = $mail_id;
					self::addID("spaarenwin_mailids",$mail_id);
					sleep(15);
					$trigger = true;
				}
			}
			if($trigger == false){
				// No new mails
				message("No new emails.", "NOTICE");
			}
			$trigger = false;
			return true;
		}
	}
	
?>
